package com.mantru.iotpot.Cleaning;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.mantru.iotpot.ApplicationClass;
import com.mantru.iotpot.R;
import com.mantru.iotpot.Tools.LoadingDialogUtil;
import com.mantru.iotpot.eventbus.EventMessage;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class PutWaterActivity extends AppCompatActivity {
    private ApplicationClass ac;
    private TextView textView;
    private Button button;
    private TextView timeText;
    private int set_time = 60; // 倒计时时间(秒)
    private CountDownTimer countDownTimer;
    private boolean isCountDownRunning = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_put_water);
        ac = (ApplicationClass) getApplication(); // 初始化 ApplicationClass

        // 初始化 UI 元素
        textView = findViewById(R.id.put_water_text);
        button = findViewById(R.id.put_water_back);
        timeText = findViewById(R.id.put_water_time_text);
        button.setOnClickListener(v -> {
//            LoadingDialogUtil.showLoadingDialog(PutWaterActivity.this);
            finish();
        });

        // 开始倒计时
        startCountDownTimer();
    }

    // 创建并启动倒计时
    private void startCountDownTimer() {
        countDownTimer = new CountDownTimer(set_time * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                // 更新 UI 显示剩余时间
                int seconds = (int) (millisUntilFinished / 1000);
                timeText.setText("剩余时间: " + seconds + "s");
            }

            @Override
            public void onFinish() {
                // 倒计时结束时的处理逻辑
                countDownTimer.cancel();
                isCountDownRunning = false;
                handleCompletion();
            }
        }.start();
        isCountDownRunning = true;
    }

    // 处理倒计时结束或接收到消息后的逻辑
    private void handleCompletion() {
        textView.setText("已完成倒水");
        button.setVisibility(View.VISIBLE);
    }

    // EventBus 解注册
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (isCountDownRunning && countDownTimer != null) {
            countDownTimer.cancel();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusMessage(EventMessage event) {
        Log.d("putwater", "EventMessage.MSG_ID_OnPotChange: " + EventMessage.MSG_ID_OnPotChange);
        if (event.id == EventMessage.MSG_ID_OnPotChange) { // 收到事件直接判断为倒水完成
            if (isCountDownRunning && countDownTimer != null) {
                countDownTimer.cancel(); // 取消倒计时
                isCountDownRunning = false;
            }
            handleCompletion(); // 执行处理逻辑
        }
    }

//    private BroadcastReceiver receiver = new BroadcastReceiver() {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            if ("ACTION_CLOSE_LOADING_DIALOG".equals(intent.getAction())) {
//                LoadingDialogUtil.closeLoadingDialog();
//            }
//        }
//    };

    // 在 Activity 可见时注册 EventBus 和本地广播接收器
    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
//        LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter("ACTION_CLOSE_LOADING_DIALOG"));
    }

    // 在 Activity 不可见时解绑 EventBus 和本地广播接收器
    @Override
    protected void onStop() {
        super.onStop();
//        LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
        EventBus.getDefault().unregister(this);
    }
}